package com.nsn.das.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.util.StringUtils;

public class DateUtils {
	
	public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy.MM.dd");

	public static String now(String pattern) {
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		return formatter.format(new Date(System.currentTimeMillis()));
	}
	
	public static String now() {		
		return DEFAULT_DATE_FORMAT.format(new Date(System.currentTimeMillis()));
	}
	
	public static Date string2Date(String time) throws ParseException{
		if(StringUtils.hasText(time)){
			return DEFAULT_DATE_FORMAT.parse(time);
		}else{
			return null;
		}
	}
	
	public static Date string2Date(String time,String pattern) throws ParseException{
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		if(StringUtils.hasText(time)){
			return formatter.parse(time);
		}else{
			return null;
		}
	}
	
	public static String date2String(Date date){
		return date != null ? DEFAULT_DATE_FORMAT.format(date) : "";
	}
	
	public static String date2String(Date date, String pattern){
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		return date != null ? formatter.format(date) : "";
	}
}
